package com.hangzhou.xyj.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.OperationRespEvent;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.CollectHandler;
import com.hangzhou.xyj.net.handler.MyLikeHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.CollectReq;
import com.hangzhou.xyj.net.request.MyLikeReq;
import com.hangzhou.xyj.ui.activity.LoginActivity;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.ToastUtil;

/**
 * Created by neo.duan on 2016/1/17.
 * 公共的分享、评论、点赞、收藏操作View
 */
public class CommonActionView extends RelativeLayout implements View.OnClickListener {
    private Context mContext;
    //    private String mLikeId = "1"; //被点赞的id：图文id或者音频id
    private int actionId; //要操作的内容Id

    public CommonActionView(Context context) {
        this(context, null);
    }

    public CommonActionView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonActionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.layout_common_action, this);

        findViewById(R.id.ll_item_share).setOnClickListener(this);
        findViewById(R.id.ll_item_comment).setOnClickListener(this);
        findViewById(R.id.ll_item_like).setOnClickListener(this);
        findViewById(R.id.ll_item_collection).setOnClickListener(this);
    }

//    public void setLikeId(String likeId) {
//        this.mLikeId = likeId;
//    }

    /**
     * 要操作内容id
     *
     * @param actionId
     */
    public void setActionId(int actionId) {
        this.actionId = actionId;
    }

    public interface OnBtnShareClickListener {
        void OnBtnShareClick();
    }

    OnBtnShareClickListener mListener;

    public void setOnBtnShareClickListener(OnBtnShareClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (XYJApplication.getUserInfo() == null) {//未登录
            IntentManager.getInstance().goActivity(mContext, new Intent(mContext, LoginActivity.class));
            return;
        }
        switch (v.getId()) {
            case R.id.ll_item_share:
                mListener.OnBtnShareClick();
                break;
            case R.id.ll_item_comment:
                IntentManager.getInstance().goCommentActivity(mContext, actionId, EIntentTagType.TAG_DRAMA_PLAY);
                break;
            case R.id.ll_item_like:
                like();
                break;
            case R.id.ll_item_collection:
                collection();
                break;
        }
    }


    /**
     * 点赞
     */
    private void like() {
        MyLikeReq req = new MyLikeReq(MyLikeReq.TYPE_DYNAMIC, Integer.valueOf(actionId), Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new MyLikeHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                showLoadingDialog();
            }

            @Override
            public void onSuccess(Object model) {
                hideLoadingDialog();
                OperationRespEvent respEvent = new OperationRespEvent();
                respEvent.setIsSuccess(true);
                EventBusUtil.postEvent(respEvent);
                ToastUtil.show("点赞成功");
            }

            @Override
            public void onFail(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onDone(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onError() {
                hideLoadingDialog();
                ToastUtil.show(getResources().getString(R.string.tip_net_error));
            }
        }, 0));
    }

    /**
     * 收藏
     */
    private void collection() {
        CollectReq req = new CollectReq(String.valueOf(actionId));
        HttpLoader.getInstance().sendRequest(new CollectHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                showLoadingDialog();
            }

            @Override
            public void onSuccess(Object model) {
                hideLoadingDialog();
                ToastUtil.show("收藏成功");
            }

            @Override
            public void onFail(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onDone(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onError() {
                hideLoadingDialog();
                ToastUtil.show(getResources().getString(R.string.tip_net_error));
            }
        }, 0));
    }

    /**
     * 显示加载框
     */
    private void showLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showLoadingDialog();
        }
    }

    /**
     * 隐藏加载框
     */
    private void hideLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).hideLoadingDialog();
        }
    }
}
